/*回复*/
function post() {
    let questionId = $("#question_id").val();
    let commentContent = $("#comment_content").val();
    comment2Target(questionId, 1, commentContent);
}

function comment2Target(id, type, commentContent) {
    if(!commentContent) {
        alert("回复内容不能为空～～～～");
        return;
    }
    $.ajax({
        type : "POST",
        url  : "/comment",
        data : JSON.stringify({
            "parentId":id,
            "type":type,
            "content":commentContent
        }),
        dataType : "json",
        contentType : "application/json",
        success : function (data) {
            if(data.code === 200) {
                // $("#comment_section").hide();
                window.location.reload();
            } else if(data.code === 2003) {
                let choose = window.confirm(data.message);
                if(choose) {
                    window.open("https://gitee.com/oauth/authorize?client_id=d81cc0d3f5803fbcfdf9d922ecd7a78644643e90fada371ff41eedd1d1897a41&redirect_uri=http://localhost:8888/callback&response_type=code");
                    window.localStorage.setItem("closeAble","true");
                }
            } else {
                alert(data.message);
            }
        }
    })

}

function comment(e) {
    var commentId = e.getAttribute("data-id");
    var content = $("#input-" + commentId).val();
    comment2Target(commentId, 2, content);
}

/**
 * 展开二级评论
 */
function collapseComments(e) {
    var id = e.getAttribute("data-id");
    var comments = $("#comment-" + id);
    // 获取二级评论展开状态
    var cStatus = e.getAttribute("data-collapse");

    if(cStatus) {
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else {
        var subCommentContainer = $("#comment-" + id);
        if(subCommentContainer.children().length !== 1) {
            comments.addClass("in");
            e.setAttribute("data-collapse","in");
            e.classList.add("active");
        } else {
            $.getJSON('/comment/' + id, function(result) {
                var data = result.data;
                if(!data) {
                    return;
                }
               $.each(data.reverse(), function(index, comment) {
                   var mediaLeftElement = $('<div/>',{
                       'class': 'media-left'
                   }).append(
                       $('<img/>',{
                           'class': 'media-object img-rounded',
                           'src': comment.user.avatarUrl
                   }));

                   var mediaBodyElement = $('<div/>',{
                       'class': 'media-body'
                   }).append(
                       $('<h5/>',{
                           'class': 'media-heading',
                           'html': comment.user.name
                       })).append(
                           $('<div/>',{
                               'html': comment.content
                       })).append(
                           $('<div/>',{
                               'class': 'menu'
                           }).append(
                               $('<span/>',{
                                   'class': 'pull-right',
                                   'html': moment(comment.gmtCreate).format('YYYY-MM-DD')
                               })));

                   var mediaElement = $('<div/>',{
                       'class': 'media'
                   }).append(mediaLeftElement).append(mediaBodyElement);


                   var commentElement = $('<div/>',{
                        'class': 'col-lg-12 col-md-12 col-sm-12 col-xs-12 comments'
                    }).append(mediaElement);

                    subCommentContainer.prepend(commentElement);
                });
                comments.addClass("in");
                e.setAttribute("data-collapse","in");
                e.classList.add("active");
            });
        }

    }
}

function selectTag(e) {
   var tagName = e.getAttribute("data-tag");
   var curTag =  $("#tag").val();
   /*if(curTag!= "" && curTag.indexOf(tagName) === -1) {
       curTag += "," + tagName;
   } else {
       curTag += tagName;
   }*/
   if(curTag) {
       if(curTag.indexOf(tagName) === -1) {
           curTag += "," + tagName;
       }
   } else {
       curTag = tagName;
   }
   $("#tag").val(curTag);
}

function showSelectTag() {
    $("#select-tag").show();
}